package cn.edu.cug.cs.gtl.apps.ui;


import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.render.Factory;
import cn.edu.cug.cs.gtl.apis.render.Render;
import cn.edu.cug.cs.gtl.apis.render.viewers.Viewer;
import cn.edu.cug.cs.gtl.apis.ui.actions.CatalogActions;
import cn.edu.cug.cs.gtl.apis.ui.actions.DeviceActions;
import cn.edu.cug.cs.gtl.apis.ui.actions.H3DFileActions;
import cn.edu.cug.cs.gtl.apis.ui.actions.HelloActions;
import cn.edu.cug.cs.gtl.apis.ui.actions.ModellingActions;
import com.formdev.flatlaf.FlatDarculaLaf;
import com.formdev.flatlaf.IntelliJTheme;
import org.pf4j.DefaultPluginManager;
import org.pf4j.PluginManager;

import javax.swing.*;
import java.io.*;
import java.util.List;

public class MainApp extends AbstractApplication {



    public MainApp(){
        mainWindow = new MainWindow(this);
    }

    public MainWindow getMainWindow() {
        return (MainWindow) mainWindow;
    }



    @Override
   public Render getRender() {
       return this.getMainWindow().getRenderPanel().getRender();
   }

   @Override
   public Viewer getViewer() {
       return this.getMainWindow().getRenderPanel().getViewer();
   }

    public static void main(String [] args){


        //https://blog.csdn.net/qq_36341729/article/details/104092758
        //https://www.formdev.com/flatlaf/themes/
        //https://www.formdev.com/flatlaf/
        try {
            //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //采用本地系统当前风格
            UIManager.setLookAndFeel(new FlatDarculaLaf());
            IntelliJTheme.setup(new FileInputStream(new File("src/main/resources/Xcode-Dark.theme.json")));
            UIManager.getDefaults().put("TabbedPane.showTabSeparators", true);
            UIManager.getDefaults().put("Tree.lineTypeDashed", true);
        } catch( Exception ex ) {
            System.err.println( "Failed to initialize LaF" );
        }


        MainApp theApp = new MainApp();

        PluginManager manager = new DefaultPluginManager();
        theApp.setPluginManager(manager);
        //加载3D渲染插件
        manager.loadPlugin(new File("D:/gitee/geospacex/plugins/render4oi-plugin/target/render4oi-plugin-1.0-SNAPSHOT.zip").toPath());
        manager.startPlugin("render4oi-plugin");
        List<Factory> fs = manager.getExtensions(Factory.class);
        for(Factory f : fs){
            theApp.factory=f;
        }

        theApp.getMainWindow().setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        theApp.getMainWindow().initial();
        theApp.getMainWindow().setSize(1024,768);
        theApp.getMainWindow().pack();
        theApp.getMainWindow().setVisible(true);

        //加载其他插件

        manager.loadPlugin(new File("D:/gitee/geospacex/plugins/h3dfile-plugin/target/h3dfile-plugin-1.0-SNAPSHOT.zip").toPath());
        manager.startPlugin("h3dfile-plugin");
        List<H3DFileActions> h3dFileActions = manager.getExtensions(H3DFileActions.class);
        for(H3DFileActions f : h3dFileActions){
            f.initialize(theApp);
        }


        manager.loadPlugin(new File("D:/gitee/geospacex/plugins/catalog-plugin/target/catalog-plugin-1.0-SNAPSHOT.zip").toPath());
        manager.startPlugin("catalog-plugin");
        List<CatalogActions> catalogActions = manager.getExtensions(CatalogActions.class);
        for(CatalogActions c : catalogActions){
            c.initialize(theApp);
        }



        manager.loadPlugin(new File("D:/gitee/geospacex/plugins/modelling-plugin/target/modelling-plugin-1.0-SNAPSHOT.zip").toPath());
        manager.startPlugin("modelling-plugin");
        List<ModellingActions> modellingActions = manager.getExtensions(ModellingActions.class);
        for(ModellingActions m : modellingActions){
            m.initialize(theApp);
        }


        manager.loadPlugin(new File("D:/gitee/geospacex/plugins/hello-plugin/target/hello-plugin-1.0-SNAPSHOT.zip").toPath());
        manager.startPlugin("hello-plugin");
        List<HelloActions> helloActions = manager.getExtensions(HelloActions.class);
        for(HelloActions h : helloActions){
            h.initialize(theApp);
        }

        manager.loadPlugin(new File("D:/gitee/geospacex/plugins/device-plugin/target/device-plugin-1.0-SNAPSHOT.zip").toPath());
        manager.startPlugin("device-plugin");
        List<DeviceActions> deviceActions = manager.getExtensions(DeviceActions.class);
        for(DeviceActions d : deviceActions){
            d.initialize(theApp);
        }
    }
}
